package UI_admin;

import outils.ExcelExporter;
import GestionBilleterie.Reservation;
import GestionPlannification.Piece;
import GestionPlannification.Representation;
import java.io.IOException;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 * Panel permettant de visualiser les reservations d'une pièce
 * @author Elsa Ferrare, Alexandre Becquet
 */
public class Panel_reservation extends javax.swing.JPanel {
    /**
     * la piece
     */
    private Piece maPiece;

    /**
     * Constructeur d'un panel pour visualiser les reservations d'une pièce
     * @param piece la pièce à correspondante
     */
    Panel_reservation(Piece piece) {        
        this.maPiece=piece;
        initComponents();
        
        List<Piece> lesRepresentations = this.maPiece.getRepresentations();
        for (int i = 0; i < lesRepresentations.size(); i++) {
            this.jComboBoxRepresentations.addItem(lesRepresentations.get(i));
        }
    }

    /**
     * Classe interne pour la création du tablau des reservations
     */
    class TableModelReservations extends AbstractTableModel {
        List<Reservation> mesReservations;
        private String[] columnNames = {"Num",
                                        "Client",
                                        "Vendeur",
                                        "Tarif",
                                        "Prix"};

        private TableModelReservations(List<Reservation> lesReservations) {
            this.mesReservations=lesReservations;
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return this.mesReservations.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
           Object value = null;
           if(col==0)
               value=this.mesReservations.get(row).getNum();
           else if(col==1)
               value=this.mesReservations.get(row).getClient();
           else if(col==2)
               value=this.mesReservations.get(row).getVendeur();
           else if(col==3)
               value=this.mesReservations.get(row).getTarif().getCouleur();
           else if(col==4)
               value=this.mesReservations.get(row).getTarif().getPrix();
           return value;
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jComboBoxRepresentations = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabelNbReservations = new javax.swing.JLabel();
        jButtonExportExcel = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jComboBoxRepresentations.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxRepresentationsActionPerformed(evt);
            }
        });

        jLabel1.setText("Representation :");

        jLabelNbReservations.setText("Nombre de réservations :");

        jButtonExportExcel.setText("Export Excel");
        jButtonExportExcel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExportExcelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxRepresentations, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelNbReservations)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                .addComponent(jButtonExportExcel))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxRepresentations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNbReservations)
                    .addComponent(jButtonExportExcel))
                .addGap(12, 12, 12)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Action du combobox des representations
     * Change la representations
     * @param evt
     */
    private void jComboBoxRepresentationsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxRepresentationsActionPerformed
        TableModelReservations tableResa=new TableModelReservations(((Representation)this.jComboBoxRepresentations.getSelectedItem()).getReservations());
        this.jTable1.setModel(tableResa);
        this.jScrollPane1.revalidate();
        this.jLabelNbReservations.setText("Nombre de réservations : "+tableResa.getRowCount());
    }//GEN-LAST:event_jComboBoxRepresentationsActionPerformed

    /**
     * Action du Button ExportExcel
     * Export du tableau en Excel et ouverture de ce fichier
     * @param evt
     */
    private void jButtonExportExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExportExcelActionPerformed
        try {
            ExcelExporter exp = new ExcelExporter("reservations.xls");
            exp.exportTable(this.jTable1);
            exp.openTable();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

}//GEN-LAST:event_jButtonExportExcelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExportExcel;
    private javax.swing.JComboBox jComboBoxRepresentations;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelNbReservations;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
